<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
        >
          <h1>HELLO</h1>
          <h3>欢迎光临硅谷甄选</h3>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_button"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '../../store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '../../utils/time'
let userStore = useUserStore()
let $router = useRouter()
let $route = useRoute()
// 收集账号和密码的数据
let loginForm = reactive({ username: 'admin', password: '111111' })
let loading = ref(false)
let loginFormRef = ref()
const login = async () => {
  // 校验通过再发请求
  await loginFormRef.value.validate() // TODO: 为什么这一行的添加看可以控制程序中断
  loading.value = true
  // 获取数据
  // 发送请求
  /*   此处加上 async await 之后就会返回值  不加就是 promise
   此处promise 不知道是在哪里定义返回过来的 */
  //    let res = userStore.userLogin(loginForm)
  //   console.log('22222222222',res);
  try {
    let res = await userStore.userLogin(loginForm)
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    // 登陆成功提示
    ElNotification({
      type: 'success',
      message: `Hi ${getTime()}好 欢迎回来`,
    })
    loading.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}
const validateUsername = (rule: any, value: any, callback: any) => {
  // rule 校验规则对象
  // value 表单元素文本内容
  // callback 控制函数
  // console.log(callback)
  //   /^\d{5,10}$/.test(value)
  if (value.length >= 5) {
    callback()
  } else {
    callback('用户名长度至少五位')
  }
}
// 1. 配置 form :model="loginForm" :rules="rules"
// 2. 配置 el-form-item： prop
// 3. 书写规则
const rules = {
  // { required: true, message: '用户名不能为空', trigger: 'blur' },
  //  required: true, 代表此项必须要校验
  //  message: '用户名不能为空', 不满足校验规则的对应提示信息
  //  trigger: 'blur', 触发校验的方式（blur:失去焦点的时候出发校验规则，change：文本发生变化时触发规则）
  username: [
    // {
    //   required: true,
    //   mix: 10,
    //   min: 6,
    //   message: '账号长度至少六位',
    //   trigger: 'blur',
    // },
    // 自定义
    {
      validator: validateUsername,
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      mix: 15,
      min: 6,
      message: '密码长度至少六位',
      trigger: 'blur',
    },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%; // 与网页一样宽
  height: 100vh; // 与视口高度一样高  此处如果是 100% 不显示
  //   background-color: red;
  //   这样配置不重复无法显示图片
  //   background-image: url('@/assets/images/background.jpg') no-repeat;
  background-image: url('@/assets/images/background.jpg');
  background-size: cover;
  background-repeat: no-repeat;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background-image: url('@/assets/images/login_form.png');
    background-size: cover;
    background-repeat: no-repeat;
    padding: 10px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h3 {
      font-size: 30px;
      color: white;
      margin: 20px 0;
    }

    .login_button {
      // 此处用 el-button 标签名控制无法实现样式设置
      width: 100%;
    }
  }
}
</style>
